<template>
  <ele-page>
    <!-- 搜索表单 -->
    <type-search @search="reload"/>
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="id"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        v-model:selections="selections"
        :highlight-current-row="true"
        :export-config="{ fileName: '报表类型管理' }"
        cache-key="jimuTypeTable"
      >
        <template #toolbar>
          <el-button
            v-permission="'jimu:type:add'"
            type="primary"
            class="ele-btn-icon"
            :icon="PlusOutlined"
            @click="openEdit()"
          >
            新增
          </el-button>
          <!--          <el-button-->
          <!--            v-permission="'jimu:type:remove'"-->
          <!--            type="danger"-->
          <!--            class="ele-btn-icon"-->
          <!--            :icon="DeleteOutlined"-->
          <!--            @click="removeBatch()"-->
          <!--          >-->
          <!--            删除-->
          <!--          </el-button>-->
          <!--          <el-button-->
          <!--            v-permission="'jimu:type:export'"-->
          <!--            class="ele-btn-icon"-->
          <!--            :icon="DownloadOutlined"-->
          <!--            @click="exportData"-->
          <!--          >-->
          <!--            导出-->
          <!--          </el-button>-->
        </template>
        <template #action="{ row }">
          <el-link
            v-permission="'jimu:type:edit'"
            type="primary"
            underline="never"
            @click="openEdit(row)"
          >
            修改
          </el-link>
          <el-divider
            v-permission="['jimu:type:edit', 'jimu:type:remove']"
            direction="vertical"
          />
          <el-link
            v-permission="'jimu:type:remove'"
            type="danger"
            underline="never"
            @click="removeBatch(row)"
          >
            删除
          </el-link>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 编辑弹窗 -->
    <type-edit v-model="showEdit" :data="current" @done="reload"/>
  </ele-page>
</template>

<script setup>
import {ref} from 'vue';
import {
  PlusOutlined,
  DeleteOutlined,
  DownloadOutlined
} from '@/components/icons';
import {ElMessageBox} from 'element-plus';
import {EleMessage} from 'ele-admin-plus';
import TypeSearch from './components/type-search.vue';
import TypeEdit from './components/type-edit.vue';
import {pageTypes, removeTypes, exportTypes} from '@/api/manage/jimu/reportType';
import {handleTree} from '@/utils/linyang.js'

defineOptions({name: 'JimuType'});

/** 表格实例 */
const tableRef = ref(null);

/** 表格列配置 */
const columns = ref([
  {
    type: 'index',
    columnKey: 'index',
    width: 50,
    align: 'center',
    fixed: 'left'
  },
  {
    prop: 'title',
    label: '类型名称',
    align: 'left',
    minWidth: 110
  },
  {
    prop: 'parentTitle',
    label: '所属父级',
    align: 'left',
    minWidth: 110
  },
  {
    prop: 'orderNum',
    label: '显示顺序',
    align: 'center',
    width: 110
  },
  {
    prop: 'remark',
    label: '备注',
    align: 'center',
    minWidth: 150
  },
  {
    columnKey: 'action',
    label: '操作',
    width: 180,
    align: 'center',
    slot: 'action',
    hideInPrint: true,
    hideInExport: true
  }
]);

/** 表格选中数据 */
const selections = ref([]);

/** 当前编辑数据 */
const current = ref(null);

/** 是否显示编辑弹窗 */
const showEdit = ref(false);

/** 表格数据源 */
const datasource = async ({pages, where}) => {
  const result = await pageTypes({...where, ...pages});
  console.log('result is', result);
  return handleTree(result.rows);
};

/** 搜索 */
const reload = (where) => {
  tableRef.value?.reload?.({page: 1, where});
};


/** 打开新增弹窗 */
// const openAdd = (row) => {
//   current.value = row ?? null;
//   showEdit.value = true;
// };

/** 打开编辑弹窗 */
const openEdit = (row) => {
  current.value = row ?? null;
  showEdit.value = true;
};

/** 批量删除 */
const removeBatch = (row) => {
  const rows = row == null ? selections.value : [row];
  if (!rows.length) {
    EleMessage.error({message: '请至少选择一条数据', plain: true});
    return;
  }
  ElMessageBox.confirm(
    `是否确认删除名称为"${rows.map((d) => d.title).join()}"的数据项?`,
    '系统提示',
    {type: 'warning', draggable: true}
  )
    .then(() => {
      const loading = EleMessage.loading({
        message: '请求中..',
        plain: true
      });
      removeTypes(rows.map((d) => d.id))
        .then(() => {
          loading.close();
          EleMessage.success({message: '删除成功', plain: true});
          reload();
        })
        .catch((e) => {
          loading.close();
          EleMessage.error({message: e.message, plain: true});
        });
    })
    .catch(() => {
    });
};

const remove = (row) => {
  const rows = [row];
  ElMessageBox.confirm(
    `是否确认删除名称为"${rows.map((d) => d.title).join()}"的数据项?`,
    '系统提示',
    {type: 'warning', draggable: true}
  )
    .then(() => {
      const loading = EleMessage.loading({
        message: '请求中..',
        plain: true
      });
      remove(rows.map((d) => d.id))
        .then(() => {
          loading.close();
          EleMessage.success({message: '删除成功', plain: true});
          reload();
        })
        .catch((e) => {
          loading.close();
          EleMessage.error({message: e.message, plain: true});
        });
    })
    .catch(() => {
    });
}

/** 导出数据 */
// const exportData = () => {
//   const loading = EleMessage.loading({
//     message: '请求中..',
//     plain: true
//   });
//   tableRef.value?.fetch?.(({where}) => {
//     exportTypes(where)
//       .then(() => {
//         loading.close();
//       })
//       .catch((e) => {
//         loading.close();
//         EleMessage.error({message: e.message, plain: true});
//       });
//   });
// };
</script>
